import * as types from './mutations_types'
import simplified_chinese from '../assets/js/international/simplified_chinese.js'
import traditional_chinese from '../assets/js/international/traditional_chinese.js'
import english from '../assets/js/international/english.js'

let hideToast = (time, callback) => {
  setTimeout(function(){
    callback();
  },time);
}

let getData = (response) => {
  return new Promise(function(resolve, reject) {
    let res = response.data;
    let success = res.success;
    let data = res.data;
    if(success) {
      if(data) {
        resolve(data);
      } else {
        resolve(success);
      }
    } else {
      reject(res);
    }
  });
}

// 控制toast的显示和自动隐藏
export const toastToggle = ({ commit }, toastSetting) => {
  let time = toastSetting.time || 1200;
  let isToasting = toastSetting.isToasting;
  commit(types.SET_TOAST, {
    info: toastSetting.info,
    icon: toastSetting.icon,
    time: time
  });
  commit(types.TOAST_TOGGLE, isToasting);
  if(isToasting) {
    hideToast(
      time,
      () => commit(types.TOAST_TOGGLE, false)
    )
  }
}

export const setLanguageVal = ({ commit }, langObj) => {
  let active = Number(langObj.active);
  let language = langObj.language;
  if(!language) {
    language = JSON.parse(window.localStorage.getItem('langObj'));
    if(!language) {
      switch (active) {
        case 0 : language = simplified_chinese;break;
        case 1 : language = traditional_chinese;break;
        case 2 : language = english;break;
        default: break;
      }
    }
  }
  commit(types.SET_SYSLANGUAGE, active);
  commit(types.SET_LANGOBJ, language);
}

// 退出登录
export const confirmLogout = ({commit}, params) => {
  return new Promise((resolve, reject) => {
    // Vue.http.post(globalUrl + 'system/logout', params).then((response) => {
    //   getData(response).then((data) => {
    //     resolve(data);
    //   }, (res) => {
    //     reject(res)
    //   })
    // }, (res) => {
    //   reject(res)
    // })
  })
}

